﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Net.Mime;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;


public class CardsEvent : MonoBehaviour
{
    //卡牌类型枚举
    public enum Enume_CardsType
    {
        /// <summary>
        /// 有XX点概率加XX点YY值,否则加XX点YY值
        /// </summary>
        A = 0,

        /// <summary>
        /// 增加XX值到XX点(范围取值)
        /// </summary>
        B = 1
    }

    //卡片物体
    public GameObject m_card;

    //卡片属性
    public Cards.AttributeCard m_attribute;

    private void Start()
    {
        if (m_card == null)
        {
            Debug.LogError("卡片没有拖放父物体");
            return;
        }

        Text rich = m_card.transform.Find("info").GetComponent<Text>();
        rich.text = Convert2CardStringA(m_attribute);
    }


    //转换常用的数值\===================================================================
    //如返回90%概率
    private string ConvertHTML2Odds(float f)
    {
        return ConvertHTMLColor(Convert.ToString(f) + "%", "#64FCB7") +
               ConvertHTMLColor("概率 ", "#666EFD");
    }

    //==================================================================================


    private string ConvertHTMLColor(string content, string color)
    {
        return string.Format("<color={0}>{1}</color>", color, content);
    }

    private string ConvertHTMLFontBold(string content)
    {
        return string.Format("<b>{0}</b>", content);
    }


    private string Convert2CardStringA(Cards.AttributeCard attribute)
    {
        string symbolHealth = attribute.MinHealth > 0 ? "+" : "";
        string symbolMood = attribute.MinMood > 0 ? "+" : "";
        string str = "";
        switch (attribute.Type)
        {
            case Cards.Enume_CardsType.A_0: //一定概率增加健康，或减少心情
                str = ConvertHTMLFontBold(attribute.Name) + "\n" +
                      ConvertHTML2Odds(attribute.Odds) + " " +
                      ConvertHTMLColor("+", "#FE0202") +
                      ConvertHTMLColor(Convert.ToString(attribute.MinHealth), "#DD04E9") +
                      ConvertHTMLColor("点", "#626AF8") +
                      ConvertHTMLColor("健康值 ", "#01EB47") +
                      ConvertHTMLColor(" 或 ", "#575DCF") +
                      ConvertHTMLColor("-", "#FE0202") +
                      ConvertHTMLColor(SubMinusSign(attribute.MinMood), "#DD04E9") +
                      ConvertHTMLColor("点", "#626AF8") +
                      ConvertHTMLColor("心情值 ", "#01EB47");
                break;
            case Cards.Enume_CardsType.A_1: //一定概率增加心情，或减少健康
                str = ConvertHTMLFontBold(attribute.Name) + "\n" +
                      ConvertHTML2Odds(attribute.Odds) + " " +
                      ConvertHTMLColor("+", "#FE0202") +
                      ConvertHTMLColor(Convert.ToString(attribute.MinMood), "#DD04E9") +
                      ConvertHTMLColor("点", "#626AF8") +
                      ConvertHTMLColor("心情值", "#01EB47") +
                      ConvertHTMLColor(" 或 ", "#575DCF") +
                      ConvertHTMLColor("-", "#FE0202") +
                      ConvertHTMLColor(SubMinusSign(attribute.MinHealth), "#DD04E9") +
                      ConvertHTMLColor("点", "#626AF8") +
                      ConvertHTMLColor("健康值 ", "#01EB47");
                break;
            case Cards.Enume_CardsType.A_2: //一定概率增加心情，或减少心情
                str = ConvertHTMLFontBold(attribute.Name) + "\n" +
                      ConvertHTML2Odds(attribute.Odds) + " " +
                      ConvertHTMLColor("+", "#FE0202") +
                      ConvertHTMLColor(Convert.ToString(attribute.MinMood), "#DD04E9") +
                      ConvertHTMLColor("点", "#626AF8") +
                      ConvertHTMLColor("心情值", "#01EB47") +
                      ConvertHTMLColor(" 或 ", "#575DCF") +
                      ConvertHTMLColor("-", "#FE0202") +
                      ConvertHTMLColor(SubMinusSign(attribute.MaxMood), "#DD04E9") +
                      ConvertHTMLColor("点", "#626AF8") +
                      ConvertHTMLColor("心情值 ", "#01EB47");
                break;
            case Cards.Enume_CardsType.A_3: //一定概率增加健康，或减少健康
                str = ConvertHTMLFontBold(attribute.Name) + "\n" +
                      ConvertHTML2Odds(attribute.Odds) + " " +
                      ConvertHTMLColor("+", "#FE0202") +
                      ConvertHTMLColor(Convert.ToString(attribute.MinHealth), "#DD04E9") +
                      ConvertHTMLColor("点", "#626AF8") +
                      ConvertHTMLColor("健康值", "#01EB47") +
                      ConvertHTMLColor(" 或 ", "#575DCF") +
                      ConvertHTMLColor("-", "#FE0202") +
                      ConvertHTMLColor(SubMinusSign(attribute.MaxHealth), "#DD04E9") +
                      ConvertHTMLColor("点", "#626AF8") +
                      ConvertHTMLColor("健康值 ", "#01EB47");
                break;

            case Cards.Enume_CardsType.B_1: //健康在范围内增加一定的值
                str = ConvertHTMLFontBold(attribute.Name) + "\n" +
                      ConvertHTMLColor("+", "#FE0202") +
                      ConvertHTMLColor(Convert.ToString(attribute.MinHealth), "#DD04E9") +
                      ConvertHTMLColor("点", "#626AF8") +
                      ConvertHTMLColor("健康值", "#01EB47") +
                      ConvertHTMLColor(" 或 ", "#575DCF") +
                      ConvertHTMLColor("+", "#FE0202") +
                      ConvertHTMLColor(Convert.ToString(attribute.MaxHealth), "#DD04E9") +
                      ConvertHTMLColor("点", "#626AF8") +
                      ConvertHTMLColor("健康值 ", "#01EB47");
                break;
            case Cards.Enume_CardsType.B_2: //心情在范围内增加一定的值
                str = ConvertHTMLFontBold(attribute.Name) + "\n" +
                      ConvertHTMLColor("+", "#FE0202") +
                      ConvertHTMLColor(Convert.ToString(attribute.MinMood), "#DD04E9") +
                      ConvertHTMLColor("点", "#626AF8") +
                      ConvertHTMLColor("心情值", "#01EB47") +
                      ConvertHTMLColor(" 或 ", "#575DCF") +
                      ConvertHTMLColor("+", "#FE0202") +
                      ConvertHTMLColor(Convert.ToString(attribute.MaxMood), "#DD04E9") +
                      ConvertHTMLColor("点", "#626AF8") +
                      ConvertHTMLColor("心情值 ", "#01EB47");
                break;
        }

        str += "\n" + ConvertHTMLColor(attribute.Descript, "#D6F702");
        return str;
    }

    //去除负号返回文本串
    private string SubMinusSign(float f)
    {
        if (f >= 0)
        {
            return Convert.ToString(f);
        }
        else
        {
            string tmp = Convert.ToString(f);
            return tmp.Substring(1);
        }
    }

    private string Convert2CardsString(Cards.AttributeCard attribute)
    {
        string name = "";
        string odds = "";
        string elseStr = "";
        string mood = "";
        string health = "";
        string descript = "";
        string symbolHealth = attribute.MinHealth > 0 ? "+" : "";
        string symbolMood = attribute.MinMood > 0 ? "+" : "";

        //物品名称
        name = ConvertHTMLFontBold(attribute.Name) + "\n";
        //判断是否需要增加概率
        if (attribute.Odds > 0)
        {
            odds = ConvertHTMLColor(Convert.ToString(attribute.Odds) + "%", "#64FCB7") +
                   ConvertHTMLColor("概率 ", "#666EFD");
            elseStr = ConvertHTMLColor(" 否则 ", "#6269F4");
        }

        //判断是否需要心情值
        if (attribute.MinMood != 0)
        {
            if (attribute.MaxMood != 0)
            {
                mood = ConvertHTMLColor(symbolMood, "#FE0202") +
                       ConvertHTMLColor(Convert.ToString(m_attribute.MinMood), "#DD04E9") +
                       ConvertHTMLColor(" ~ ", "#FFFFFF") +
                       ConvertHTMLColor(symbolMood, "#FE0202") +
                       ConvertHTMLColor(Convert.ToString(m_attribute.MaxMood), "#DD04E9") +
                       ConvertHTMLColor("点", "#626AF8") +
                       ConvertHTMLColor("心情值", "#01EB47");
            }
            else
            {
                mood = ConvertHTMLColor(symbolMood, "#FE0202") +
                       ConvertHTMLColor(Convert.ToString(m_attribute.MinMood), "#DD04E9") +
                       ConvertHTMLColor("点", "#626AF8") +
                       ConvertHTMLColor("心情值", "#01EB47");
            }
        }

        //判断是否需要健康值
        if (attribute.MinHealth != 0)
        {
            if (attribute.MaxHealth != 0)
            {
                health = ConvertHTMLColor(symbolHealth, "#FE0202") +
                         ConvertHTMLColor(Convert.ToString(m_attribute.MinHealth), "#DD04E9") +
                         ConvertHTMLColor(" ~ ", "#FFFFFF") +
                         ConvertHTMLColor(symbolHealth, "#FE0202") +
                         ConvertHTMLColor(Convert.ToString(m_attribute.MaxHealth), "#DD04E9") +
                         ConvertHTMLColor("点", "#626AF8") +
                         ConvertHTMLColor("健康值", "#01EB47");
            }
            else
            {
                health = ConvertHTMLColor(symbolHealth, "#FE0202") +
                         ConvertHTMLColor(Convert.ToString(m_attribute.MinHealth), "#DD04E9") +
                         ConvertHTMLColor("点", "#626AF8") +
                         ConvertHTMLColor("健康值", "#01EB47");
            }
        }

        //描述文字
        descript = "\n" + ConvertHTMLColor(attribute.Descript, "#D6F702");
        string res = name + odds + mood + elseStr + health + descript;
        return res;
    }


    //从商店购买卡片,并放置到我的背包
    public void BuyCard()
    {
        CardsControl.Instance.PlaceCard2Package(this.gameObject);
    }

    //使用一个卡片
    public void UseCard()
    {
        
        
        
        //使用卡片后加减数值
        float val = 0f;
        string temp = "";
        switch (m_attribute.Type)
        {
            case Cards.Enume_CardsType.A_0: //一定概率增加健康，或减少心情
                if (Random.Range(1, 100) > m_attribute.Odds)
                {
                    //加健康
                    BooldControl.Instance.Health += m_attribute.MinHealth;
                    if (m_attribute.MinHealth > 0)
                    {
                        temp = " +" + m_attribute.MinHealth + "点健康值";
                    }
                    else
                    {
                        temp = " " + m_attribute.MinHealth + "点健康值";
                    }

                    ShowInfo(m_attribute.Radion1 + temp);
                }
                else
                {
                    //减心情
                    BooldControl.Instance.Mood += m_attribute.MinMood;

                    if (m_attribute.MinMood > 0)
                    {
                        temp = " +" + m_attribute.MinMood + "点心情值";
                    }
                    else
                    {
                        temp = " " + m_attribute.MinMood + "点心情值";
                    }

                    ShowInfo(m_attribute.Radion2 + temp);
                }

                break;
            case Cards.Enume_CardsType.A_1: //一定概率增加心情，或减少健康
                if (Random.Range(1, 100) > m_attribute.Odds)
                {
                    //加心情
                    BooldControl.Instance.Mood += m_attribute.MinMood;

                    if (m_attribute.MinMood > 0)
                    {
                        temp = " +" + m_attribute.MinMood + "点心情值";
                    }
                    else
                    {
                        temp = " " + m_attribute.MinMood + "点心情值";
                    }

                    ShowInfo(m_attribute.Radion1 + temp);
                }
                else
                {
                    //减健康
                    BooldControl.Instance.Health += m_attribute.MinHealth;

                    if (m_attribute.MinHealth > 0)
                    {
                        temp = " +" + m_attribute.MinHealth + "点健康值";
                    }
                    else
                    {
                        temp = " " + m_attribute.MinHealth + "点健康值";
                    }

                    ShowInfo(m_attribute.Radion2 + temp);
                }

                break;
            case Cards.Enume_CardsType.A_2: //一定概率增加心情，或减少心情
                if (Random.Range(1, 100) > m_attribute.Odds)
                {
                    //加心情
                    BooldControl.Instance.Mood += m_attribute.MaxMood;

                    if (m_attribute.MaxMood > 0)
                    {
                        temp = " +" + m_attribute.MaxMood + "点心情值";
                    }
                    else
                    {
                        temp = " " + m_attribute.MaxMood + "点心情值";
                    }

                    ShowInfo(m_attribute.Radion1 + temp);
                }
                else
                {
                    //减心情
                    BooldControl.Instance.Mood += m_attribute.MinMood;

                    if (m_attribute.MinMood > 0)
                    {
                        temp = " +" + m_attribute.MinMood + "点心情值";
                    }
                    else
                    {
                        temp = " " + m_attribute.MinMood + "点心情值";
                    }

                    ShowInfo(m_attribute.Radion2 + temp);
                }

                break;
            case Cards.Enume_CardsType.A_3: //一定概率增加健康，或减少健康
                if (Random.Range(1, 100) > m_attribute.Odds)
                {
                    //加健康
                    BooldControl.Instance.Health += m_attribute.MaxHealth;

                    if (m_attribute.MaxHealth > 0)
                    {
                        temp = " +" + m_attribute.MaxHealth + "点健康值";
                    }
                    else
                    {
                        temp = " " + m_attribute.MaxHealth + "点健康值";
                    }

                    ShowInfo(m_attribute.Radion1 + temp);
                }
                else
                {
                    //减健康
                    BooldControl.Instance.Health += m_attribute.MinHealth;

                    if (m_attribute.MinHealth > 0)
                    {
                        temp = " +" + m_attribute.MinHealth + "点健康值";
                    }
                    else
                    {
                        temp = " " + m_attribute.MinHealth + "点健康值";
                    }

                    ShowInfo(m_attribute.Radion2 + temp);
                }

                break;
            case Cards.Enume_CardsType.B_1: //健康在范围内增加一定的值
                val = Random.Range(m_attribute.MinHealth, m_attribute.MaxHealth);
                BooldControl.Instance.Health += val;

                if (val > 0)
                {
                    temp = " +" + val + "点健康值";
                }
                else
                {
                    temp = " " + val + "点健康值";
                }

                ShowInfo(m_attribute.Radion1 + temp);
                break;
            case Cards.Enume_CardsType.B_2: //心情在范围内增加一定的值
                val = Random.Range(m_attribute.MinMood, m_attribute.MaxMood);
                BooldControl.Instance.Mood += val;

                if (val > 0)
                {
                    temp = " +" + val + "点心情值";
                }
                else
                {
                    temp = " " + val + "点心情值";
                }

                ShowInfo(m_attribute.Radion1 + temp);
                break;
        }

        //获取新卡片数值,直接替换卡片数值
        m_attribute = ReadCard.Instance.RandomMy(BooldControl.Instance.Health, BooldControl.Instance.Mood);
        Text rich = m_card.transform.Find("info").GetComponent<Text>();
        rich.text = Convert2CardStringA(m_attribute);

        //使用之后增加使用次数,当次数累积5次时会置零,并且会增加骰子数量
        CardsControl.Instance.OwnCardCount++;
    }

    //卡片对两条血的影响===================================

    //加减健康值
    private void AddHealth(float num)
    {
        BooldControl.Instance.Health += num;
    }

    //加减心情值
    private void AddMood(float num)
    {
        BooldControl.Instance.Mood += num;
    }

    private void ShowInfo(string content)
    {
        GameObject.Find("Canvas/Panel/infoView").GetComponent<Gameinfo>().Getinfo(content);
    }

    //====================================================
}